//
//  AppDelegate.m
//  待定
//
//  Created by BoyaLee on 15/11/4.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "AppDelegate.h"
#import "TBCRadioViewController.h"
#import "LeftViewController.h"
#import "TBCRootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
#pragma mark -- 给App土包添加3D Touch菜单
    // 拍照
    // 菜单图标
    UIApplicationShortcutIcon *iconCamera = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    // 菜单文字
    UIMutableApplicationShortcutItem *itemCamera = [[UIMutableApplicationShortcutItem alloc] initWithType:@"1" localizedTitle:@"拍照"];
    // 绑定信息到制定菜单
    itemCamera.icon = iconCamera;
    
    // 相册
    // 菜单图标
    UIApplicationShortcutIcon *iconPhotoLibrary = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
    // 菜单文字
    UIMutableApplicationShortcutItem *itemPhotoLibrary = [[UIMutableApplicationShortcutItem alloc] initWithType:@"2" localizedTitle:@"相册"];
    // 绑定信息到制定菜单
    itemPhotoLibrary.icon = iconPhotoLibrary;
    
    // 绑定到App icon
    application.shortcutItems = @[itemCamera, itemPhotoLibrary];
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
#pragma mark -- 判断是否第一次进入这个APP,如果是,在沙盒里面存放一个文件,再次打开这个APP时如果有次文件,则不用加载引导页
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *tbcTxt = [documentPath stringByAppendingPathComponent:@"tbc.txt"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    BOOL isHasFile = [manager fileExistsAtPath:tbcTxt];
    
    if (isHasFile) {
        
#pragma mark -- 为真表示已有文件 曾经进入过主页
        [self initRootViewControllers];
        
    }else {
        
#pragma mark -- 为假表示没有文件，没有进入过主页
        TBCRootViewController * rootVC = [[TBCRootViewController alloc] init];
        [[self window] setRootViewController:rootVC];
        
        isHasFile = YES;
        
    }
    
    return YES;
    
}

- (void)initRootViewControllers {
    
    TBCRadioViewController *radioVC = [[TBCRadioViewController alloc] init];
    UINavigationController *radioNaVC = [[UINavigationController alloc] initWithRootViewController:radioVC];
    radioVC.navigationItem.title = @"电台";
    
    self.menuController = [[DDMenuController alloc] initWithRootViewController:radioNaVC];
    LeftViewController *leftVC = [[LeftViewController alloc] init];
    self.menuController.leftViewController = leftVC;
    self.window.rootViewController = self.menuController;
    
#pragma mark -- 图片扩大淡出的效果开始;
    
#pragma mark -- 设置一个图片;
    UIImageView * launchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [launchImageView setBackgroundColor:[UIColor whiteColor]];
    [launchImageView setImage:[UIImage imageNamed:@"LaunchImage@2x"]];
    
#pragma mark -- 添加到场景
    [[self window] addSubview:launchImageView];
    
#pragma mark -- 放到最顶层;
    [[self window] bringSubviewToFront:launchImageView];
    
#pragma mark -- 开始设置动画;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:[self window] cache:YES];
    [UIView setAnimationDelegate:self];
    
    [launchImageView setAlpha:0.0];
    [UIView commitAnimations];
    
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    
    // 照相type
    if ([shortcutItem.type isEqualToString:@"1"]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];// 初始化
        picker.allowsEditing = YES;// 设置可编辑
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self.window.rootViewController presentViewController:picker animated:YES completion:nil];// 进入照相
        
    }
    //相册type
    if ([shortcutItem.type isEqualToString:@"2"]) {
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];// 初始化
        picker.allowsEditing = YES;// 设置可编辑
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.window.rootViewController presentViewController:picker animated:YES completion:nil];// 进入图片库
        
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
